#include "Person.h"
#include "NSJoint.h"
#include "NSJointLoad.h"
#include <objbase.h>
#include <string>
#include <sstream>
using namespace std;
extern "C" __declspec(dllexport) Person* CreatePerson()
{
	return new Person();
}

extern "C" __declspec(dllexport) Person* SetPersonAge(Person& person, double age)
{
	person.age = age;
	std::cout << "c++personadress:" << (int)&person << std::endl;
	std::cout << "c++personageadress:" << (int)&(person.age) << std::endl;
	return &person;
}

extern "C" __declspec(dllexport) void SetPersonAge2(Person* person, double age)
{
}

//extern "C" __declspec(dllexport) void SetJob(Person * person, Job job)
//{
//	person->SetJob(job);
//}

extern "C" __declspec(dllexport) int PersonEat(Person* person)
{
	return person->Eat();
}
extern "C" __declspec(dllexport) void PersonDelete(Person * person)
{
	delete person;
}

unsigned long* g_pnStatus = 0;
extern "C" __declspec(dllexport) unsigned char __cdecl someMethod(unsigned long* nStatus)
{
	g_pnStatus = nStatus;

	return 0;
}
extern "C" __declspec(dllexport) void SetTeacher(Teacher * person)
{
	person->Call();
}
extern "C" __declspec(dllexport) void SetTeacherP(TeacherP * person)
{
	person->Call();
}
//extern "C" __declspec(dllexport) int AddPerson(Person* person)
//{
//	Person& newPerson = person->Add();
//	newPerson.age=20;
//	Person* pa = &newPerson;
//	if (pa)
//	{
//		return 1;
//	}
//	else
//	{
//		return 0;
//	}
//}
extern "C" __declspec(dllexport) NSJoint * NewJoint( )
{
	NSJoint* jointPtr= new NSJoint();
	return jointPtr;
}
extern "C" __declspec(dllexport) void SetJointLoad(NSJoint* jointPtr,NSJointLoad* loadPtr)
{
	jointPtr->jointLoad = loadPtr;
	jointPtr->jointLoad = loadPtr;
	//NSJointLoad* load = new NSJointLoad();
	//load->id = loadPtr->id;
	//jointPtr->jointLoad = load;
	std::cout << "C++ set load id:" << jointPtr->jointLoad->id << std::endl;
	std::cout << "C++ set load fx:" << jointPtr->jointLoad->Fx << std::endl;
	std::cout << "C++ size:" << sizeof(NSJointLoad) << std::endl;
	std::cout << "C++ size empty:" <<sizeof(NSModelBase)<< std::endl;
}
extern "C" __declspec(dllexport) NSJointLoad* GetJointLoad(NSJoint * jointPtr)
{
	std::cout << "C++ get id:" << jointPtr->jointLoad->id << std::endl;
	std::cout << "C++ get fx:" <<jointPtr->jointLoad->Fx << std::endl;
	return jointPtr->jointLoad;
}

extern "C" __declspec(dllexport) void SetJointLoadArr(NSJoint * jointPtr,int count, NSJointLoad * loadPtrs[])
{
	for (int i = 0; i < count; i++)
	{
		std::cout << "C++ set arr load id:" << loadPtrs[i]->id<< std::endl;
		std::cout << "C++ set arr load fx:" << (int)loadPtrs[i]->Fx << std::endl;
		std::cout << "C++ set arr load fY:" << (int)loadPtrs[i]->Fy << std::endl;
		NSJointLoad* dd = new NSJointLoad();
		std::cout << "C++ new addr" << (int)dd << std::endl;
	}
	
	
}

extern "C" __declspec(dllexport) void SetJointStrArr(NSJoint * jointPtr, int count, char* str[])
{
	for (int i = 0; i < count; i++)
	{
		std::cout << "C++ set str:" << str[i]<< std::endl;
	}


}

extern "C" __declspec(dllexport) void SetJointSection(NSJoint * jointPtr, RectangleSection * section)
{
	jointPtr->rectangleSection = section;
	//NSJointLoad* load = new NSJointLoad();
	//load->id = loadPtr->id;
	//jointPtr->jointLoad = load;
	std::cout << "C++ size:" << sizeof(RectangleSection) << std::endl;
	std::wstring ws(section->name);
	std::string str(ws.begin(), ws.end());
	std::wcout << "C++ name:" << str.c_str() << std::endl;
	std::cout << "C++ width:" << section->GetWidth() << std::endl;
	std::cout << "C++ height:" << section->GetHeight() << std::endl;


	std::cout << "C++ wchar_t* size:" << sizeof(wchar_t*) << std::endl;
	std::cout << "C++ double size:" <<sizeof(double)<< std::endl;

	std::cout << "c++ STRING SIZE:" << sizeof(std::string*) << std::endl;

	double* sectionType = (double*)section;
	double* width = (double*)(sectionType+1);
	std::cout << "C++ width2:" << *width<< std::endl;
	double* height = (double*)(sectionType +2);
	std::cout << "C++ height2:" << *height << std::endl;
}


static void GuidToBytes(GUID guid, unsigned char guidBytes[16])
{
	CopyMemory(guidBytes, &guid.Data1, 4);
	CopyMemory(guidBytes + 4, &guid.Data2, 2);
	CopyMemory(guidBytes + 6, &guid.Data3, 2);
	for (int i = 0; i < 8; i++)
	{
		guidBytes[8 + i] = guid.Data4[i];
	}
}

static GUID BytesToGuid(unsigned char guidBytes[])
{
	GUID guid;
	CoCreateGuid(&guid);
	if (!guidBytes)
		return guid;

	unsigned long d1;
	unsigned short d2;
	unsigned short d3;
	CopyMemory(&d1, guidBytes, 4);
	CopyMemory(&d2, guidBytes + 4, 2);
	CopyMemory(&d3, guidBytes + 6, 2);
	for (size_t i = 0; i < 16; i++)
	{
		if (i > 7)
		{
			guid.Data4[i - 8] = guidBytes[i];
		}
	}
	std::cout << "CPPGUID:";
	guid.Data1 = d1;
	guid.Data2 = d2;
	guid.Data3 = d3;

	return guid;
}
extern "C" __declspec(dllexport) void TestPVGuid(unsigned char guidBytes[])
{
	GUID guid = BytesToGuid(guidBytes);
	wchar_t test[48] = { 0 };
	StringFromGUID2(guid, test, sizeof(test));
	wprintf(L"btg:");
	wprintf(test);

	unsigned char bytes[16];
	GuidToBytes(guid, bytes);

	guid = BytesToGuid(bytes);
	StringFromGUID2(guid, test, sizeof(test));
	wprintf(L"\ngtb:");
	wprintf(test);

	
}

